package com.qiaomengapp.dao;

import java.util.Date;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.qiaomengapp.bean.Color;
import com.qiaomengapp.bean.GoodTypelevel1;
import com.qiaomengapp.bean.GoodTypelevel2;
import com.qiaomengapp.bean.ProviderGood;
import com.qiaomengapp.bean.ShopGood;
import com.qiaomengapp.bean.ShopGoodWrap;
import com.qiaomengapp.bean.Size;

public interface ShopGoodMapper {
	// 通过shop_good_id来修改商品的上架状态1-上架,同时更新上架时间
	@Update("update shop_good set status=1 where id=#{value}")
	int update_statusTo1(int id);

	@Insert("update shop_good set launch_time=now() where id=#{value}")
	int update_launch_time(int id);

	// 下架
	@Update("update shop_good set status=2 where id=#{value}")
	int update_statusTo2(int shit_id);

	// 显示销售中的商品
	List<ShopGood> select_ShopGood(int shop_id);
	//显示待售的商品
	List<ShopGood> select_ShopGoodStatus2(int shop_id);
	// 显示销售的商品并按销量排序
	List<ShopGood> select_ShopGoodBySaleCount(int shop_id);

	// 显示销售的商品并按佣金排序
	List<ShopGood> select_ShopGoodByCommission(int shop_id);

	// 显示销售的商品并按售价排序
	List<ShopGood> select_ShopGoodByPrice(int shop_id);

	// 显示销售的商品并按收藏排序
	List<ShopGood> select_ShopGoodByfocus_num(int shop_id);

	// 显示一级分类
	@Select("select id,name from good_typelevel1")
	List<GoodTypelevel1> select_GoodTypelevel1();

	// 点击一级分类显示二级分类
	@Select("select g2.id,g2.name from good_typelevel2 g2 INNER JOIN good_typelevel1 g1 on g2.good_level1id=g1.id where g1.id=#{value}")
	List<GoodTypelevel2> select_GoodTypelevel2(int id);

	// 点击二级分类获取所有二级分类的商品
	List<ShopGood> select_G2_good(int goodTypelevel2Id);

	// 删除商品
	@Delete("delete from shop_good where id=#{value}")
	int delete_shopGood(int shopGoodId);

	// 查看商品的详情
	ShopGoodWrap select_ShopGoodWrap(int id);

	// 添加商品,前台页面显示店铺可添加的provide_good的信息，在shopgood表中添加相应的数据.......修改
	@Insert("insert into shop_good(provider_good_id,sum,price,commission,good_desc,good_imgurl,good_level2id,status,size_id,color_id,sales_count,shop_id)values(#{pro.id},#{pro.sum},#{pro.price},#{pro.commission},#{pro.goodDesc},#{pro.goodImgurl},#{pro.goodLevel2Id},2,#{pro.sizeId},#{pro.colorId},#{pro.salesCount},#{shop_id})")
	int insert_Good(@Param("pro") ProviderGood pro,
			@Param("shop_id") int shop_id);

	// 收藏商品,从session取出登陆用户的id，获取收藏商品的id，shopid
	// 1.在user_focus表中添加一条记录
	@Insert("insert into user_focus(user_id,shop_good_id) values (#{a},#{b})")
	int insert_user_focus(@Param("a") int user_id, @Param("b") int shop_good_id);

	// 2.把shopgood中的相应id的focus_num的值加 1
	@Update("update shop_good set focus_num=focus_num+1 where id=#{value}")
	int update_addshopGoodFocusNum(int id);

	// 收藏店铺
	@Insert("insert into user_focus(user_id,shop_id) values(#{a},#{b})")
	int insert_shop(@Param("a") int user_id, @Param("b") int shop_id);

	@Update("update shop set collect_num=collect_num+1 where id=#{value}")
	int update_shop_addcollectNum(int id);

	// 显示商品的参数,color,size
	List<Color> select_color();

	@Select("select size from size")
	List<Size> select_size();

	// 店铺预览，显示所有的商品数量
	@Select("select count(id) from shop_good where shop_id=#{value}")
	int select_shopGoodNum(int shop_id);

	// 显示上架新品的数量====在最近一个月内上架的商品
	@Select("select launch_time from shop_good where status=1")
	List<Date> select_launchTime();

	// 使用datediff来计算上架日期在一个月内的商品，需要字符串日期，使用Mysql中的datefiff函数来计算和当前时间的天数差
	@Select("select DATEDIFF(now(),#{value})")
	int select_shopGoodByTime(String time);
	
	/**
	 *@Description描述: 修改商品总数量
	 *@param sum 商品总数量
	 *@param shopGoodId 商铺商品id
	 *@return
	 *@return int 影响行数
	 */
	@Update("update shop_good set sum=sum+#{sum} where id=#{shopGoodId} and sum>=-#{sum}")
	int update_shopGoodSum(@Param("sum") int sum,@Param("shopGoodId") int shopGoodId);
	
	/**
	 *@Description描述: 修改商品销量
	 *@param count  销出去的数量
	 *@param shopGoodId 销出去的商铺商品id
	 *@return
	 *@return int 影响的行数
	 */
	@Update("update shop_good set sales_count=sales_count+#{sales_count} where id=#{shopGoodId} and sales_count>=-#{sales_count}")
	int update_shopGoodSalesCount(@Param("sales_count") int count,@Param("shopGoodId") int shopGoodId);
}
